<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * date: 2021-06-03 18:30:49
 * @LastEditors: Sam
 * @LastEditTime: 2021-12-09 22:13:48
 */


namespace app\__MODULE__\controller;
use app\__MODULE__\model\ {User,Notice as NoticeModel,NoticeType,Attach};
use think\facade\Db;

// 首页
class Notice extends Base{
    
    public function index(){
        if(IS_POST) {
            $search = \request()->param();
            $where  = [];
            if(isset($search['title']) && $search['title'] != '')  $where[] = ['title','like',"%{$search['title']}%"];  
            if(isset($search['type']) && $search['type'] != '')  $where[] = ['type','=',$search['type']];  
            $db = NoticeModel::with('type')->where($where)->field('id,title,type,sort,thumb,create_time,is_top,is_show')->order('sort desc,id desc');
            $list = $db->limit(paginator())->select();
            foreach ($list as $key => $value) {
               $value['thumb'] = table_img($value['thumb']);
               $value['is_top']  = table_switch('__MODULE__/Notice/is_top',['id'=>$value['id']],$value['is_top']==1?'cheked':'','是|否');
               $value['is_show']  = table_switch('__MODULE__/Notice/is_show',['id'=>$value['id']],$value['is_show']==1?'cheked':'','是|否');
            }
            admin_response(0, 'success', $list, $db->count());
        }
        return $this->render_table([
            'page'   => true,
            'cols'   => [
                ['field'=>'title', 'title'=>'标题','align'=>'center'],
                ['field'=>'thumb', 'title'=>'图片','align'=>'center'],
                ['field'=>'type_text', 'title'=>'类型','align'=>'center','align'=>'center'],
                ['field'=>'is_top', 'title'=>'是否置顶','align'=>'center'],
                ['field'=>'is_show', 'title'=>'是否显示','align'=>'center'],
                ['field'=>'create_time', 'title'=>'发布时间','align'=>'center'],
            ],
            'toolbar' => [
                table_button('__MODULE__/Notice/add','添加','frame','',['1200px','800px']),
            ],
            'toolbar_row' => [
                table_button('__MODULE__/Notice/edit', '编辑','frame','',['1200px','800px']),
                table_button('__MODULE__/Notice/del', '删除', 'confirm'),
            ],
            'where' => [
                'title' => ['type'=>'input','title'=>'标题'],
                'type'  => ['type'=>'select','title'=>'分类','option'=>NoticeType::field('id as value,title')->select()]
            ]
            ],false);
        return $this->fetch();
    }
    /**
     * 设置显示
     * @return void
     * @author Sam
     * date 2021-09-29
     */
    public function is_show(){
        $id      = param_check('id');
        $checked = param_check('checked', 0);
        $res = NoticeModel::where('id', $id)->update([
            'is_show'        => $checked ? 1 : 0
        ]);
        $res ? json_response(1, '修改成功') : json_response(0, '修改失败');
    }

    /**
     * 设置置顶
     * @return bool
     * @author Sam
     * date 2021-12-09
     */
    public function is_top(){
        $id      = param_check('id');
        $checked = param_check('checked', 0);
        $res = NoticeModel::where('id', $id)->update([
            'is_top'        => $checked ? 1 : 0
        ]);
        $res ? json_response(1, '修改成功') : json_response(0, '修改失败');
    }
    
    
    /**
     * 添加
     * @return void
     * @author Sam
     * date 2021-06-04
     */
    public function add(){
        if(request()->isPost()) {
           $data = request()->post();
           if(NoticeModel::create($data)) json_response(1,'操作成功');
           json_response(0,'操作失败');
        }else{
            $type  = NoticeType::field('id as value,title')->select();
            return $this->render_form([
                ['type'=>'text', 'name'=>'title', 'title'=>'标题', 'lay-verify'=>'required'],
                ['type'=>'select','name'=>'type', 'title'=>'类型','option'=>$type],
                ['type'=>'textarea','name'=>'sub_title', 'title'=>'摘要'],
                ['type'=>'upload','name'=>'thumb', 'title'=>'缩略图'],
                ['type'=>'editor','name'=>'detail', 'title'=>'详情'],
             
               
                [
                    ['type'=>'text','text_type'=>'number','name'=>'sort', 'title'=>'排序'],
                    ['type'=>'radio','name'=>'is_show', 'title'=>'是否显示','option'=>[
                        ['title'=>'是','value'=>1],
                        ['title'=>'否','value'=>0],
                    ]],
                    ['type'=>'radio','name'=>'is_top', 'title'=>'是否置顶','option'=>[
                        ['title'=>'否','value'=>0],
                        ['title'=>'是','value'=>1]
                    ]]
                ]
               
            ]);
        }
    }

    /**
     * 编辑
     * @return void
     * @author Sam
     * date 2021-06-04
     */
    public function edit(){
        $id = param_check('id');
        if(request()->isPost()) {
           $data = request()->post();
           if(NoticeModel::update($data,['id'=>$id])) json_response(1,'操作成功');
           json_response(0,'操作失败');
        }else{
            $data  = NoticeModel::get($id);
            $type  = NoticeType::field('id as value,title')->select();
            return $this->render_form([
                ['type'=>'text', 'name'=>'title', 'title'=>'标题', 'lay-verify'=>'required'],
                ['type'=>'select','name'=>'type', 'title'=>'类型','option'=>$type],
                ['type'=>'textarea','name'=>'sub_title', 'title'=>'摘要'],
                ['type'=>'upload','name'=>'thumb', 'title'=>'缩略图'],
                ['type'=>'editor','name'=>'detail', 'title'=>'详情'],
             
               
                [
                    ['type'=>'text','text_type'=>'number','name'=>'sort', 'title'=>'排序'],
                    ['type'=>'radio','name'=>'is_show', 'title'=>'是否显示','option'=>[
                        ['title'=>'是','value'=>1],
                        ['title'=>'否','value'=>0],
                    ]],
                    ['type'=>'radio','name'=>'is_top', 'title'=>'是否置顶','option'=>[
                        ['title'=>'否','value'=>0],
                        ['title'=>'是','value'=>1]
                    ]]
                ]
               
            ],$data);
        }
    }

        /**
     * 删除
     * @return void
     * @author Sam
     * date 2021-06-04
     */
    public function del(){
        $id = param_check('id');
        if(!$level = NoticeModel::get($id)) json_response(0,'不存在');
        if($level->delete()) json_response(1,'操作成功');
        json_response(0,'操作失败');
    }

}